package com.sum.tea.rongrui

import android.os.*
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import com.blankj.utilcode.constant.PermissionConstants
import com.blankj.utilcode.util.PermissionUtils
import com.sum.common.base.BaseMvvmActivity
import com.sum.tea.R
import com.sum.tea.databinding.ActivityRw6cBinding
import com.sum.tea.rongrui.viewmodel.CheckType
import com.sum.tea.rongrui.viewmodel.ReadWriteViewModel

class ReadWriteActivity : BaseMvvmActivity<ActivityRw6cBinding, ReadWriteViewModel>() , View.OnClickListener, AdapterView.OnItemSelectedListener {

    override fun initView(savedInstanceState: Bundle?) {
        initView()
        checkAndRequestPermission()

        // 观察 ViewModel 中的 LiveData
        mViewModel.result.observe(this) { result ->
            mBinding.rwResult.text = result
        }
    }

    private fun initView() {
        with(mBinding) {
            epcText.text = ScanModeActivity.epc

            // 设置 Spinner 适配器
            val memAdapter = ArrayAdapter.createFromResource(
                this@ReadWriteActivity,
                R.array.men_select,
                android.R.layout.simple_spinner_item
            ).apply {
                setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
            }
            memSpinner.adapter = memAdapter
            memSpinner.setSelection(3, true)
            memSpinner.onItemSelectedListener = this@ReadWriteActivity

            val lockMemAdapter = ArrayAdapter.createFromResource(
                this@ReadWriteActivity,
                R.array.arrayLockMem,
                android.R.layout.simple_spinner_item
            ).apply {
                setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
            }
            lockmemSpinner.adapter = lockMemAdapter
            lockmemSpinner.setSelection(4, true)

            val lockTypeAdapter = ArrayAdapter.createFromResource(
                this@ReadWriteActivity,
                R.array.arrayLock,
                android.R.layout.simple_spinner_item
            ).apply {
                setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
            }
            locktypeSpinner.adapter = lockTypeAdapter
            locktypeSpinner.setSelection(2, true)

            // 设置默认值
            etWordptr.setText("0")
            etLength.setText("6")
            etPwd.setText("00000000")

            // 设置按钮点击监听
            buttonRead6c.setOnClickListener(this@ReadWriteActivity)
            buttonWrite6c.setOnClickListener(this@ReadWriteActivity)
            buttonWriteEpc.setOnClickListener(this@ReadWriteActivity)
            buttonKill.setOnClickListener(this@ReadWriteActivity)
            buttonLock.setOnClickListener(this@ReadWriteActivity)
            buttonLed.setOnClickListener(this@ReadWriteActivity)
            buttonTemp.setOnClickListener(this@ReadWriteActivity)
        }
    }

    override fun onResume() {
        super.onResume()
        mBinding.epcText.text = ScanModeActivity.epc
    }

    override fun onClick(view: View) {
        when (view) {
            mBinding.buttonRead6c -> {
                if (!mViewModel.checkContent(CheckType.CHECK_W_6C, mBinding)) return
                mViewModel.writeData(
                    epc = mBinding.epcText.text.toString(),
                    mem = mBinding.memSpinner.selectedItemPosition.toByte(),
                    wordPtr = mBinding.etWordptr.text.toString().toInt().toByte(),
                    password = mBinding.etPwd.text.toString(),
                    data = mBinding.etContent6c.text.toString()
                )
            }
            mBinding.buttonWrite6c -> {
                if (!mViewModel.checkContent(CheckType.CHECK_R_6C, mBinding)) return
                mViewModel.readData(
                    epc = mBinding.epcText.text.toString(),
                    mem = mBinding.memSpinner.selectedItemPosition.toByte(),
                    wordPtr = mBinding.etWordptr.text.toString().toInt(),
                    num = mBinding.etLength.text.toString().toInt(),
                    password = mBinding.etPwd.text.toString()
                )
            }
            mBinding.buttonWriteEpc -> {
                if (!mViewModel.checkContent(CheckType.CHECK_W_6C, mBinding)) return
                mViewModel.writeEPC(
                    password = mBinding.etPwd.text.toString(),
                    data = mBinding.etContent6c.text.toString()
                )
            }
            mBinding.buttonKill -> {
                mViewModel.killTag(
                    password = mBinding.etKwd.text.toString(),
                    epc = mBinding.epcText.text.toString(),
                    isU9Lock = mBinding.checku9.isChecked,
                    lockMem = mBinding.lockmemSpinner.selectedItemPosition.toByte(),
                    lockType = mBinding.locktypeSpinner.selectedItemPosition.toByte()
                )
            }
            mBinding.buttonLock -> {
                mViewModel.lockTag(
                    password = mBinding.etPwd.text.toString(),
                    epc = mBinding.epcText.text.toString(),
                    lockMem = mBinding.lockmemSpinner.selectedItemPosition.toByte(),
                    lockType = mBinding.locktypeSpinner.selectedItemPosition.toByte(),
                    isU9Lock = mBinding.checku9.isChecked
                )
            }
            mBinding.buttonLed -> {
                mViewModel.toggleLED(
                    password = mBinding.etPwd.text.toString(),
                    epc = mBinding.epcText.text.toString()
                )
            }
            mBinding.buttonTemp -> {
                mViewModel.readTemperature(
                    password = mBinding.etPwd.text.toString(),
                    epc = mBinding.epcText.text.toString()
                )
            }
        }
    }

    override fun onItemSelected(parent: AdapterView<*>, view: View?, position: Int, id: Long) {
        mViewModel.selectedEd = position
    }

    override fun onNothingSelected(parent: AdapterView<*>) {
        // 不需要处理
    }

    private fun checkAndRequestPermission() {
        PermissionUtils.permissionGroup(PermissionConstants.STORAGE)
            .callback(object : PermissionUtils.FullCallback {
                override fun onGranted(permissionsGranted: List<String>) {

                }

                override fun onDenied(permissionsDeniedForever: List<String>,
                                      permissionsDenied: List<String>) {
                }
            })
            .request()
    }

}

